package com.ok.common.ext

import android.graphics.drawable.GradientDrawable
import android.os.Handler
import android.os.Looper
import android.widget.LinearLayout
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

fun LinearLayout.shapeBackground(cornerRadius: Float = 3f.dp, @ColorRes colorResId: Int) {
    try {
        val color = ContextCompat.getColor(context, colorResId)
        val shape = createRoundedRectangle(color, cornerRadius)
        background = shape
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun LinearLayout.shapeBackground(cornerRadius: Float = 3f.dp, hexColor: String) {
    try {
        val color = android.graphics.Color.parseColor(hexColor)
        val shape = createRoundedRectangle(color, cornerRadius)
        background = shape
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

private fun createRoundedRectangle(color: Int, cornerRadius: Float): GradientDrawable {
    val shape = GradientDrawable()
    shape.shape = GradientDrawable.RECTANGLE
    shape.setColor(color)
    shape.cornerRadius = cornerRadius
    return shape
}
